﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using BCCommon;
using BCDto.Common.Banner;
using BCService.Common.Banner;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace BCTMSRESTful.Controllers.Management.Common.Banner
{
    /// <summary>
    /// Banner信息控制器
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class BannerController : ManagementBaseController
    {
        private readonly IBannerService bannerService;

        public BannerController(IBannerService bannerService)
        {
            this.bannerService = bannerService;
        }

        /// <summary>
        /// 添加Banner信息。
        /// </summary>
        /// <param name="dto">Banner请求信息</param>
        /// <response code="201">已添加成功的Banner信息。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加Banner")]
        [ProducesResponseType(typeof(BannerDto), 200)]
        public IActionResult CreateBanner([FromBody]BannerRequestDto dto)
        {
            BannerDto result = this.bannerService.Add(dto);
            return this.Created(string.Format("/api/v1/sim/Banner/{0}", result.BannerId), result);
        }


        /// <summary>
        /// 修改Banner信息。
        /// </summary>
        /// <param name="bannerId">BannerId</param>
        /// <param name="dto">Banner信息</param>
        /// <response code="200">返回修改成功的Banner标记码信息。</response>
        /// <returns></returns>
        [HttpPut("{banner_id}")]
        [Log("修改Banner")]
        [ProducesResponseType(typeof(BannerDto), 200)]
        public IActionResult Put([FromRoute(Name = "banner_id")]long bannerId, [FromBody]BannerRequestDto dto)
        {
            return this.Ok(this.bannerService.Update(bannerId, dto));
        }
        /// <summary>
        /// 获取Banner信息列表。
        /// </summary>
        /// <param name="bannerTagcode">Banner分类编码</param>
        /// <response code="200">返回Banner信息列表</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<BannerDto>), 200)]
        public IActionResult GetList([FromQuery(Name = "banner_tagcode")]string bannerTagcode)
        {
            return this.Ok(this.bannerService.Get(bannerTagcode));
        }
        /// <summary>
        /// 获取Banner信息列表（分页）。
        /// </summary>
        /// <param name="name">Banner名称</param>
        /// <param name="bannerTagcode">Banner分类编码</param>
        /// <param name="ascending">排序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">每页数据条数</param>
        /// <response code="200">返回Banner信息列表（分页）</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<BannerDto>), 200)]
        public IActionResult GetPageableList([FromQuery(Name = "name")]string name,
            [FromQuery(Name = "banner_tagcode")]string bannerTagcode,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10,
            [FromQuery(Name = "ascending")]bool? ascending = null)
        {
            return this.Ok(this.bannerService.GetPageableList(name, bannerTagcode, ascending, index, size));
        }
        /// <summary>
        /// 获取Banner信息。
        /// </summary>
        /// <param name="bannerId">BannerId</param>
        /// <response code="200">返回Banner信息</response>
        /// <returns></returns>
        [HttpGet("{banner_id}")]
        [ProducesResponseType(typeof(BannerDto), 200)]
        public IActionResult Get([FromRoute(Name = "banner_id")]long bannerId)
        {
            return this.Ok(this.bannerService.GetEntity(bannerId));
        }

        /// <summary>
        /// 删除Banner信息。
        /// </summary>
        /// <param name="bannerId">BannerId</param>
        /// <response code="204">删除成功</response>
        /// <returns></returns>
        [HttpDelete("{banner_id}")]
        [Log("删除Banner")]
        public IActionResult Delete([FromRoute(Name = "banner_id")]long bannerId)
        {
            this.bannerService.Delete(bannerId);
            return this.NoContent();
        }

        #region 启用/禁用Banner
        /// <summary>
        ///  启用/禁用Banner
        /// </summary>
        /// <param name="bannerId">BannerId</param>
        /// <param name="enabled">是否启用</param>
        /// <response code="204">启用/禁用Banner成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("[action]/{banner_id}/{enabled}")]
        [Log("启用/禁用Banner")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Enabled([FromRoute(Name = "banner_id")]long bannerId,
            [FromRoute(Name = "enabled")]bool enabled)
        {
            if (enabled)
            {
                this.bannerService.Enabled(bannerId);
                return StatusCode((int)HttpStatusCode.NoContent);
            }
            else
            {
                this.bannerService.Disabled(bannerId);
                return StatusCode((int)HttpStatusCode.NoContent);
            }
        }
        #endregion
    }
}
